package com.lv.service;


import com.lv.bean.User;
import com.lv.dao.UserDao;
import com.lv.dao.UserDaoImpl;
import com.lv.utils.BaseUtils;

import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao dao = new UserDaoImpl();

    @Override
    public List<User> select(User user) {
        StringBuilder sql=new StringBuilder("select * from user where 1=1 ");
        List<Object> list=new ArrayList<>();
        if(user!=null){
            //根据编号查询
            if(user.getUserid()!=null && !"".equals(user.getUserid())){
                sql.append(" and id = ? ");
                list.add(user.getUserid());
            }

            //根据名称模糊查询,模糊查询"" %% "" ++
            if(user.getName()!=null && !"".equals(user.getName())){
                sql.append(" and name like ? ");
                list.add("%"+user.getName()+"%");
            }
        }

        return dao.select(sql.toString(), list);
    }

    /*
      新增用户信息
      */
    @Override
    public boolean insertUser(User user) {
        String sql = "insert into user (userid,name,active,avatar," +
                "department,mobile,admin) values(?,?,?,?,?,?,?);";

        List<Object> list = new ArrayList<>();
        if (user != null) {
            list.add(user.getUserid());
            list.add(user.getName());
            list.add(user.isActive()+"");
            list.add(user.getAvatar());
            list.add(user.getDepartment());
            list.add(user.getMobile());
            list.add(user.getAdmin());
        }
        try {
            return BaseUtils.addUpdateDelete(sql, list.toArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /*
         根据userId修改用户信息
     */
    @Override
    public boolean updateUser(User user) {
       String sql="update user set admin=? where userid=?;";
        List<Object> list=new ArrayList<>();
        if(user!=null){
            list.add(user.getAdmin());
            //根据编号修改信息
            list.add(user.getUserid());
        }

        try {
            return BaseUtils.addUpdateDelete(sql, list.toArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /*
      删除用户信息
     */
    @Override
    public boolean deleteUser(String userId) {
        String sql = "delete from user where userid=? ";
        List<Object> list = new ArrayList<>();
        if (userId != null&&!("".equals(userId))) {
            //根据编号修改信息
            list.add(userId);
        }

        try {
            return BaseUtils.addUpdateDelete(sql, list.toArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<User> selectOfId(User user) {
        String  sql="select * from user where userid=?;";
        List<Object> list=new ArrayList<>();
        if(user!=null){
            //根据编号查询
            if(user.getUserid()!=null && !"".equals(user.getUserid())){
                list.add(user.getUserid());
            }
        }
        return dao.select(sql, list);
    }


}
